﻿using Obligatorio2013.Persistencia;
using Obligatorio2013.Clases;
using Obligatorio2013.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using System.Xml.Linq;


namespace Obligatorio2013.Persistencia.Daos
{
    public class CategoryDAO : PersistentDAO
    {


        private string categoryFileName = "categorias.xml";


        public override bool Save(object obj)
        {
            try
            {
                if (!FileExist(categoryFileName))
                {
                    createFileXml(path, categoryFileName, "Categories");
                }

                if (obj != null)
                {
                    // Crear un nuevo elemento 
                    Category cat = obj as Category;

                    XmlDocument doc = LoadXmlFile(path, categoryFileName);
                    XmlElement newElement = doc.CreateElement("Category");

                    addAttribute(doc, newElement, "name", cat.Name);
                    // Crear los nodos hijos.
                    addElement(doc, newElement, "id", cat.CatId.ToString());

                    if (cat.KeyWords != null)
                    {
                        string aux = null;
                        foreach (var key in cat.KeyWords)
                        {
                            if (!String.IsNullOrEmpty(key))
                            {
                                if (aux == null)
                                { aux = key + ";"; }
                                else
                                { aux = aux + key + ";"; }
                            }
                        }
                        addElement(doc, newElement, "keys", aux);
                    }

                    // agregar el elemento al final de la lista de elementos.
                    doc.DocumentElement.AppendChild(newElement);
                    doc.Save(path + this.categoryFileName);
                    return true;
                }

            }
            catch (Exception ex)
            {
                Log.Instance.Error("Error salvando Categoria",ex.StackTrace);
                throw ex;
            }
            return false;
        }


        public override List<Object> getAll()
        {
            XmlReader reader = XmlReader.Create(path + "\\" + categoryFileName);
            List<Object> categories;

            XElement element = XElement.Load(reader, LoadOptions.SetBaseUri);

            IEnumerable<XElement> items = element.DescendantsAndSelf("Category");

            if (reader != null)
            {
                categories = new List<Object>();

                foreach (var xElement in items)
                {
                    Category cat = new Category();
                    cat.Name = GetAttributeValue("name", xElement);
                    cat.CatId = Int32.Parse(GetElementValue("id", "string", xElement));

                    String aux = GetElementValue("keys", "string", xElement);
                    cat.KeyWords = aux.Split(';').ToList();
                    categories.Add(cat);

                }

                return categories;
            }


            throw new System.NotImplementedException();
        }


        //private static string GetElementValue(string elementName, string attributeName, XElement element)
        //{
        //    XElement xElement = element.Element(elementName);

        //    string value = string.Empty;

        //    if (xElement != null)
        //    {
        //        value = xElement.Value;
        //    }

        //    return value;
        //}
        //private static string GetAttributeValue(string attributeName, XElement element)
        //{
        //    XAttribute xAttribute = element.Attribute(attributeName);

        //    string value = string.Empty;
        //    if (xAttribute != null)
        //    {
        //        value = xAttribute.Value;
        //    }

        //    return value;
        //}



        public override bool Update(Object claveAnterior)
        {
            throw new System.NotImplementedException();
        }

        public override bool Delete(Object clave)
        {
            throw new System.NotImplementedException();
        }
    }
}